<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="登入账号"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton text="登 录" :rotate="isRotate" @click.native="startLogin()"></wButton>

			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" open-type="getUserInfo" @getuserinfo="login_weixin" withCredentials="true"></view>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import request from '../../util/request.js'

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
			//this.isLogin();
		},
		methods: {
			isLogin() {
				const userInfo = this.cache.get("user_info");
				if (userInfo) this.toUserHome();
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码格式不正确'
					});
					return;
				}
				_this.isRotate = true // 设置按钮加载中
				request.post('/wechat/user/login', {
					username: _this.phoneData,
					password: _this.passData
				}).then(res => {
					console.log(res)
					if (res.code === 200) {

						// 登入成功
						// 存入缓存
						_this.cache.put("token", res.data.token, 60 * 60 * 2); // token两小时有效
						_this.cache.put("user_info", res.data.user, 60 * 60 * 2); // 用户信息两小时有效
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: '登入成功'
						});
						this.toUserHome();
					}else{
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: '用户名或者密码错误'
						});
						_this.isRotate = false
					}
				})


				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
			},
			login_weixin(res) {
				//微信登录
				let _this = this;
				if (!res.detail.iv) {
					uni.showToast({
						title: "您取消了授权,登录失败",
						icon: "none"
					});
					return false;
				}
				console.log('-------用户授权，并获取用户基本信息和加密数据------')
				console.log(res.detail);
			},
			toUserHome() {
				uni.redirectTo({
					url: "/pages/my/my"
				})
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
